home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / bsrc_p2.arc / MDM_PROC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-30  |  11.1 KB  |  378 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------            KopyRong (K) 1987.  ALL RIGHTS REVERSED.       */
  9. /*                                                                          */
  10. /*                                                                          */
  11. /*               This module was written by Vince Perriello                 */
  12. /*                                                                          */
  13. /*                                                                          */
  14. /*                    BinkleyTerm Modem Handler Module                      */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*   This  software  package is being distributed WITH FULL SOURCE CODE     */
  18. /*   with the  following  conditions:    1)  If  anything awful happens     */
  19. /*   because  you  use    it   (or  don't  use  it),  you  accept  full     */
  20. /*   responsibility;  2) you  don't start making tons of voice calls to     */
  21. /*   the authors to complain or  make  suggestions  about enhancements,     */
  22. /*   useful or otherwise;  3) you  do not reuse this code in commercial     */
  23. /*   products without specific permission to do so  from  the  authors;     */
  24. /*   4) If you find any problems you send  fixes  to  the  authors  for     */
  25. /*   inclusion  in  updates;    5) You find some way  to  express  your     */
  26. /*   appreciation  for  this  method of distribution, either by writing     */
  27. /*   code or  application  notes,  or  just sending along a "Thank You"     */
  28. /*   message.                                                               */
  29. /*                                                                          */
  30. /*   There is  copyrighted  code  in  this product.  We either wrote it     */
  31. /*   ourselves or got  permission  to use it.  Please don't force us to     */
  32. /*   pay a lawyer --  have some respect for our motives and don't abuse     */
  33. /*   this "license".                                                        */
  34. /*                                                                          */
  35. /*                                                                          */
  36. /*--------------------------------------------------------------------------*/
  37.  
  38. #include <stdio.h>
  39. #include <signal.h>
  40. #include <ctype.h>
  41. #include <conio.h>
  42. #include "com.h"
  43. #include "xfer.h"
  44.  
  45. extern struct pointers ctl;            /* where all the strings is */
  46. extern struct baud_str btypes[];        /* the baud numbers, strings */
  47. extern int caller;
  48. extern int port_ptr;
  49. extern int baud;
  50. extern int un_attended;
  51. extern int fullscreen;
  52. extern int cur_event;
  53.  
  54. int slowmodem = 0;
  55.  
  56.  
  57. #define FAILURE 0
  58. #define IGNORE    1
  59. #define SUCCESS 2
  60.  
  61. struct resp_str {
  62.     char *resp;
  63.     unsigned disp;
  64.     };
  65.  
  66. static struct resp_str mdm_resps[] = {
  67.     {    "CONNECT",    2 },
  68.     {    "RING",        1 },
  69.     {    "RRING",    1 },
  70.    {  "RINGING", 1 },
  71.     {    "BUSY",        0 },
  72.     {    "VOICE",    0 },
  73.     {    "ERROR",    0 },
  74.     {    "OK",        0 },
  75.     {    "NO CARRIER",    0 },
  76.     {    "NO DIAL TONE",    0 },
  77.    {  "DIAL TONE", 0 },
  78.     {    NULL,        0 }
  79.     };
  80.  
  81. static char *response_string = "                    ";
  82.  
  83. try_2_connect(phnum)
  84. char *phnum;
  85. {
  86. long t1,timerset();
  87. int k;
  88.  
  89. while (!KEYPRESS())    /* get out if key pressed */
  90.     {
  91.     k = dial_modem(phnum);
  92.     if (k || KEYPRESS())
  93.         break;
  94.     t1 = timerset(200);
  95.     while (!timeup(t1) && !KEYPRESS())
  96.         time_release();    /* pause for 2 seconds */
  97.     }
  98. if (!k)
  99.     {
  100.     status_line("!Connection attempt aborted");
  101.    if (!caller)
  102.        FOSSIL_CHAR();
  103.     mdm_hangup();
  104.     }
  105. /*scr_printf("\r\n");
  106. locate_x = wherex();
  107. locate_y = wherey();*/
  108. }
  109.  
  110. try_1_connect(phnum)
  111. char *phnum;
  112. {
  113. long t1,timerset();
  114. int k;
  115.  
  116. if (!dial_modem(phnum))
  117.     {
  118.     mdm_hangup();
  119.     }
  120. /*    scr_printf("\r\n");
  121.     locate_x = wherex();
  122.     locate_y = wherey();*/
  123. }
  124.  
  125. dial_modem(number)
  126. char *number;
  127. {
  128. long t;
  129. extern long timerset();
  130.  
  131. if (*number == '\"')                /* If it's a script          */
  132.     return(do_script(number));        /* then do it that way       */
  133.  
  134. status_line(":Dialing %s",number);
  135. if (un_attended && fullscreen)
  136.    {
  137.    gotoxy (69, 7);
  138.    cprintf ("%-2d Dialing", cur_event + 1);
  139.    }
  140.  
  141. DTR_OFF();                    /* drop DTR to reset modem   */
  142. timer(20);                    /* leave it down 2 seconds   */
  143. DTR_ON();                    /* then raise DTR again      */
  144. timer(5);                    /* and wait .5 sec for modem */
  145. mdm_cmd_char(CR);                /* clean up any mess         */
  146. mdm_cmd_string(ctl.predial,0);            /* transmit the dial prefix  */
  147. mdm_cmd_string(number,0);            /* then the phone number     */
  148. mdm_cmd_string(ctl.postdial,0);            /* finally the dial suffix   */
  149. mdm_cmd_char(CR);                /* terminate the string      */
  150. CLEAR_INBOUND();
  151. if (modem_response(7500))            /* we got a good response,   */
  152.     {
  153.     timer(20);                /* wait for other side       */
  154.     return(CARRIER);            /* Carrier should be on now  */
  155.     }
  156. return(0);                    /* no good */
  157. }
  158.  
  159. char *get_response(end_time)
  160. long end_time;                    /* timeout parameters        */
  161. {
  162. char *p = response_string;            /* points to character cell  */
  163. char c;                        /* current modem character   */
  164. int count = 0;                    /* count of characters       */
  165. while ((count < 20)                /* until we have 20 chars,   */
  166.    && (!timeup(end_time))            /* or out of time,           */
  167.    && (!KEYPRESS()))                /* or user gets impatient    */
  168.     {
  169.     if (!CHAR_AVAIL())            /* if nothing ready yet,     */
  170.         {
  171.         time_release();
  172.         continue;            /* just process timeouts     */
  173.         }
  174.     c = MODEM_IN();                /* get a character           */
  175.     if (c == '\r' || c == '\n')        /* if a line ending          */
  176.         {
  177.         if (count != 0)            /* and we have something,    */
  178.             break;            /* get out                   */
  179.         else
  180.             continue;        /* otherwise just keep going */
  181.         }
  182.     *p++ = c;                /* store the character       */
  183.     ++count;                /* increment the counter     */
  184.     }
  185. *p = '\0';                    /* terminate the new string  */
  186.  
  187. if (count != 0)
  188.     {
  189.     fancy_str(response_string);        /* make it pretty            */
  190.     status_line("\#%s",response_string);    /* pop it out on the screen  */
  191.     }
  192.  
  193. return(response_string);            /* return the pointer        */
  194. }
  195.  
  196. parse_response(response)
  197. char *response;
  198. {
  199. char *p;                    /* temp character pointer    */
  200. register int i;                        /* array pointer             */
  201. for (i = 0; mdm_resps[i].resp != NULL; i++)            /* scan through array        */
  202.     {
  203.     p = mdm_resps[i].resp;            /* point at possible response*/
  204.     if (strnicmp(response,p,strlen(p)) == 0)/* if a match,               */
  205.         return(mdm_resps[i].disp);    /* return disposition of it  */
  206.     }
  207. return(1);                    /* ignore all unknowns       */
  208. }
  209.  
  210.  
  211. modem_response(ths)
  212. int ths;                    /* millisecs to wait         */
  213. {
  214. long end_time;                    /* holds time at end of 2min */
  215. extern long timerset();
  216.  
  217. char *response;                    /* pointer to modem response */
  218. char *c,*skip_blanks();                /* miscellaneous pointer     */
  219. int result = IGNORE;                /* result code               */
  220.  
  221. end_time = timerset(ths);            /* arm the timeout           */
  222. while ((result == IGNORE)             /* until success or failure, */
  223.    && (!timeup(end_time))            /* or out of time,           */
  224.    && (!KEYPRESS()))                 /* or user gets impatient    */
  225.     {
  226.     response = get_response(end_time);    /* get a response            */
  227.     result = parse_response(response);    /* parse, determine status   */
  228.     time_release();
  229.     }
  230.  
  231. if (result == SUCCESS)                /* Got to be a CONNECT msg   */
  232.     {
  233.     if (strnicmp(response,"connect",7) == 0)/* if this is a CONNECT,     */
  234.         {
  235.         c = skip_blanks(&response[7]);    /* get past the blanks       */
  236.         if (*c == '\0')            /* if nothing there,         */
  237.             set_baud(300,1);    /* say that it's 300 baud    */
  238.         else
  239.             set_baud(atoi(c),1);    /* else do baudrate fallback */
  240.         }
  241.     }
  242.     
  243. return (result == SUCCESS);            /* timeout or failure or OK  */
  244. }
  245.  
  246. mdm_cmd_string(mdm_cmd,dospace)
  247. char *mdm_cmd;
  248. int dospace;
  249. {
  250. register char *c;
  251. for (c = mdm_cmd; *c; c++)
  252.     {
  253.     if (dospace || !isspace(*c))        /* don't output spaces       */
  254.         mdm_cmd_char(*c);        /* output the next character */
  255.     }
  256. }
  257.  
  258. empty_delay ()
  259. {
  260. long t, timerset ();
  261.  
  262. t = timerset (500);
  263. while ((!OUT_EMPTY()) && (!timeup(t)))
  264.     time_release();                /* wait for output to finish */
  265. if (!OUT_EMPTY())
  266.     {
  267.     status_line ("!Timeout waiting for output to clear");
  268.     MDM_DISABLE();
  269.     Cominit(port_ptr);
  270.     MDM_ENABLE(btypes[baud].rate);
  271.     DTR_ON();
  272.     CLEAR_OUTBOUND();
  273.     CLEAR_INBOUND();
  274.     }
  275. }
  276.  
  277. mdm_cmd_char(outchr)
  278. char outchr;
  279. {
  280.  
  281. if (outchr == '\-')                /* if it's a dash (phone no) */
  282.     return;                    /* ignore it                 */
  283.  
  284. if (outchr == '\|')                /* if the CR character,      */
  285.     outchr = CR;                /* substitute a real CR here */
  286.  
  287. if (outchr == '.')                /* Substitute ',' for '.' for compatibility */
  288.     outchr = ',';
  289.  
  290. if (outchr == '\~')                /* if the "delay" character, */
  291.     {
  292.     empty_delay ();
  293.     timer(10);                /* then wait 1 second        */
  294.     return;                    /* and return                */
  295.     }
  296.  
  297. if (outchr == '^')                /* Raise DTR */
  298.     {
  299.     empty_delay ();
  300.     DTR_ON ();
  301.     return;
  302.     }
  303.  
  304. if (outchr == 'v')         /* Lower DTR */
  305.     {
  306.     empty_delay ();
  307.     DTR_OFF ();
  308.     return;
  309.     }
  310.  
  311. if (outchr == '`')                /* Short delay */
  312.     {
  313.     timer(1);                /* short pause, .1 second    */
  314.     return;
  315.     }
  316.  
  317. SENDBYTE(outchr);                /* then write the character  */
  318.  
  319. if (outchr == CR)                /* if it was a CR,           */
  320.     {
  321.     empty_delay ();
  322.     timer(1);                /* allow .1 sec line quiet   */
  323.     }
  324. else if (slowmodem)
  325.     {
  326.     timer(1);                /* wait .1 sec for output    */
  327.     }
  328. }
  329.  
  330. mdm_hangup()
  331. {
  332. /* First, if a dial command is in progress,
  333.    try to get the modem to abort it...                               */
  334.  
  335. CLEAR_OUTBOUND();
  336. CLEAR_INBOUND();
  337.  
  338. if (un_attended && fullscreen)
  339.    {
  340.    gotoxy (69, 7);
  341.    cprintf ("%-2d HangUp ", cur_event + 1);
  342.    }
  343. else
  344.    {
  345.    status_line("#Modem hang up sequence");  /* Tell what we are doing */
  346.    }
  347.  
  348. mdm_init(ctl.m_init);            /* re-initialize the modem   */
  349.  
  350. DTR_OFF();                /* Then turn off DTR again   */
  351. timer(5);                /* And hold for .5 sec more  */
  352.  
  353. DTR_ON();                /* Raise it,                 */
  354. mdm_cmd_char(CR);            /* then another CR           */
  355. set_xy("");
  356. mdm_cmd_char(CR);            /* then another CR           */
  357. CLEAR_INBOUND();            /* then flush input and exit */
  358. }
  359.  
  360. mdm_init(str)
  361. {
  362. CLEAR_OUTBOUND();
  363. CLEAR_INBOUND();
  364. mdm_cmd_char(CR);            /* output a CR, then         */
  365. DTR_OFF();                /* Drop DTR to hangup        */
  366. timer(10);                /* Hold it down for 1 sec    */
  367.  
  368. DTR_ON();                /* Raise DTR,                */
  369. timer(5);                /* Then hold it up for .5 sec*/
  370. set_baud (ctl.max_baud, 0);
  371.  
  372. mdm_cmd_char(' ');            /* output a space            */
  373. mdm_cmd_char(CR);            /* then another CR           */
  374. mdm_cmd_string(str,0);            /* then the modem init string*/
  375. timer(5);                /* Hold DTR for .5 sec more  */
  376. CLEAR_INBOUND();            /* then flush input and exit */
  377. }
  378.